package com.System.servlet;

import com.System.pojo.*;
import com.System.service.Bar;
import com.System.service.BarImpl.BarService;
import com.System.service.Post;
import com.System.service.PostImpl.PostService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.text.SimpleDateFormat;
import java.util.List;

@WebServlet("/Post/*")
public class PostServlet extends BaseServlet{
    Post service=new PostService();
    public void selectMyPost(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = request.getParameter("username");
        User user=new User(username,null);
        com.System.pojo.Post MyPost = service.selectMyPost(user);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 将 bar 对象转换为 JSONObject
        // 格式化创建时间并添加到 JSONObject 中
        String createdTime = dateFormat.format(MyPost.getCreated_at());
        MyPost.setCreated_at(null);
        JSONObject barObject = (JSONObject) JSON.toJSON(MyPost);
        barObject.put("created_at", createdTime);

        // 将整合后的 JSONObject 转换为字符串输出
        String jsonString = barObject.toJSONString();

        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    /**
     * 添加点赞操作的时候要执行的逻辑
     * @param request
     * @param response
     * @throws Exception
     */
    public void likePost(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        String username = request.getParameter("username");
        String postId = request.getParameter("postId");
        int id=Integer.parseInt(postId);

        Favor target=null;
        target=new   Favor(username,barName,id);
        Favor result=null;
        result=service.hasFavor(target);
        //查找有没有点赞过
        if(result==null||result.getBarName()==null){
            //查找有没有给过差评
            DisFavor res = service.hasDisFavor(new DisFavor(username, barName, id));
            if(res!=null){
                service.deleteDisFavor(res);
            }
            //更新post的点赞数进行+1的操作
            com.System.pojo.Post temp = service.selectLikeNumbers(new com.System.pojo.Post(id,username,barName));
            temp.setLikeNumber(temp.getLikeNumber()+1);

            service.updateLikeNumber(temp);
            //添加点赞数据
            service.likePost(new Favor(username,barName,id));
            response.getWriter().write("success");
        }else{
            response.getWriter().write("hasAdd");
        }

    }
    public void deleteFocus(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        String username = request.getParameter("username");
        BufferedReader reader = request.getReader();
        String param = reader.readLine();
        com.System.pojo.Post post = JSON.parseObject(param, com.System.pojo.Post.class);
        service.deleteMyPost(post);
        response.getWriter().write("success");
    }
    public void addPost(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        String username = request.getParameter("username");
        Bar temp=new BarService();
        Ban ban = temp.hasBan(new Ban(username, barName));
        if(ban==null){
        BufferedReader reader = request.getReader();
        String param = reader.readLine();
        com.System.pojo.Post post = JSON.parseObject(param, com.System.pojo.Post.class);
        post.setBarName(barName);
        post.setName(username);
        post.setLikeNumber(0);
        service.addPost(post);
        response.getWriter().write("success");}
        else{
            response.getWriter().write("error");
        }
    }
    public void deleteMyPost(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        String username = request.getParameter("username");
        BufferedReader reader = request.getReader();
        String param = reader.readLine();
        com.System.pojo.Post post = JSON.parseObject(param, com.System.pojo.Post.class);
        post.setName(username);
        post.setBarName(barName);
        Report report=new Report();
        report.setBarName(barName);
        report.setUserName(post.getName());
        report.setPostId(post.getId());
        Bar service1=new BarService();
        Report temp = service1.hasReport(report);
        if(temp!=null){
            service1.deleteReport(temp);
        }
        service.deleteMyPost(post);
        response.getWriter().write("success");
    }

    /**
     * 根据postId来获取对应的post信息
     * @param request
     * @param response
     * @throws Exception
     */
    public void selectOne(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String postId = request.getParameter("postId");
        int id = Integer.parseInt(postId);
        com.System.pojo.Post post = service.selectOne(new com.System.pojo.Post(id));
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 将 bar 对象转换为 JSONObject
        // 格式化创建时间并添加到 JSONObject 中
        String createdTime = dateFormat.format(post.getCreated_at());
        post.setCreated_at(null);
        JSONObject barObject = (JSONObject) JSON.toJSON(post);
        barObject.put("created_at", createdTime);

        // 将整合后的 JSONObject 转换为字符串输出
        String jsonString = barObject.toJSONString();

        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);

    }
    public void goToCommentHtml(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        String username = request.getParameter("username");
        BufferedReader reader = request.getReader();
        String param = reader.readLine();
        com.System.pojo.Post post = JSON.parseObject(param, com.System.pojo.Post.class);
        int postId = post.getId();
        String ref="http://localhost:8080/BarSystem/Comment.html?username="+username+"&barName="+barName+"&postId="+postId+"";
        String jsonString = JSON.toJSONString(ref);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    public void updatePost(HttpServletRequest request, HttpServletResponse response) throws Exception {
        BufferedReader reader = request.getReader();
        String param = reader.readLine();
        com.System.pojo.Post post = JSON.parseObject(param, com.System.pojo.Post.class);
        service.updatePost(post);
        response.getWriter().write("success");
    }
    public void addDisFavor(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        String username = request.getParameter("username");
        String postId = request.getParameter("postId");
        int id=Integer.parseInt(postId);

        DisFavor target=null;
        target=new   DisFavor(username,barName,id);
        DisFavor result=null;
        result=service.hasDisFavor(target);
        //如果之前没有过差评记录就走以下程序
        if(result==null||result.getBarName()==null){
            Favor favor = service.hasFavor(new Favor(username, barName,id));
            if(favor!=null){
                //修改点赞数据
                com.System.pojo.Post temp = service.selectLikeNumbers(new com.System.pojo.Post(id,username,barName));
                int ans = temp.getLikeNumber() - 1;
                temp.setLikeNumber(ans);
                service.updateLikeNumber(temp);
                service.unLikePost(favor);
            }
            service.addDisFavor(new DisFavor(username,barName,id));
            response.getWriter().write("success");
        }else{
            response.getWriter().write("hasAdd");
        }
    }
    }
